import md5 from 'blueimp-md5';
import { Buffer } from 'buffer';
import * as api from './api';
import { request, secretKey, setRequestUrl } from './config';
import { isNumeric,isEmpty, } from "../utils/untils";



// 基础请求方法
const baseRequest = async (url: string, params = new Map()) => {
  if (!url || !api[url]) {
    return await {
      code: 404,
      msg: '请求的地址不在配置文件中',
      data: []
    };
  }
  url = api[url];
  return await request.post(url, params);
}

// 登录
export const login = async (userName: string, password:string) => {
  const data = new Map();
  data.set('userName', new Buffer(userName).toString('base64'));
  data.set('password', md5(password));  //md5(password))
  return await baseRequest('LOGIN', data);
}

// 登出
export const logout = async (user_id: string) => {
  const data = new Map();
  return await baseRequest('LOGOUT', data);
}

// 待配送列表
export const getDispatchingList = async (page: number, limit: number,search?: string) => {
  const data = new Map();
  if(!isEmpty(search)){
    if(isNumeric(search)){
      data.set('codeID', search);
    }else{
      data.set('address',search);
    }
  }
  data.set('page', page);
  data.set('limit', limit);
  data.set('orderStatus', "4000");
  // data.set('toBeSend',1); // 可配送
  return await baseRequest('DISPATCHINGLIST', data);
}

// 通过订单号查询生产单列表
export const getFactoryListByOrder = async (codeID: string,otherOrders?: string) => {
  const data = new Map();
  data.set('codeID', codeID);
  data.set('activekey', 10);
  data.set('page', 1);
  data.set('limit', 999999);
  if(otherOrders){
    data.set('otherOrders',otherOrders);  // 外协单
  }
  return await baseRequest('FACTORY_LIST', data);
}

// 通过订单配料单列表
export const getPickingListByOrder = async (codeID: string) => {
  const data = new Map();
  data.set('codeID',codeID);
  data.set('orderType',20);
  data.set('page', 1);
  data.set('limit', 999999);
  return await baseRequest('PICKING_LIST', data);
}
// 包装清单出库
export const updateInOrOutPicking = async (factoryCodeID: string,packagesNum: string) => {
  const data = new Map();
  data.set('factoryCodeID', factoryCodeID);
  data.set('packagesNum', packagesNum);
  data.set('isSingle',1)
  return await baseRequest('UPDATE_PICKING_ITEM__STATUS', data);
}

// 生产单包装检索
export const getPackagesList = async (factoryCodeID: string) => {
  const data = new Map();
  data.set('factoryOrderID', factoryCodeID);
  data.set('status', 20); // 未入库10，已入库未出库20，已出库 30
  return await baseRequest('PICKINGS_LIST', data);
}

// 入库
export const putInStorage = async (factoryOrderID: string) => {
  const data = new Map();
  data.set('factoryOrderID', factoryOrderID);
  data.set('factoryOrderStatus', 30);
  return await baseRequest('UPDATE_ORDER', data);
}
// 发货
export const updateOrderStatus = async (codeID: string) => {
  const data = new Map();
  data.set('codeID', codeID);
  data.set('orderStatus', 5000);
  return await baseRequest('UPDATE_ORDER_STATUS', data);
}







// 修改密码
export const resetPassword = async (oldPassword: string, newPassword: string) => {
  const data = new Map();
  data.set('password', md5(oldPassword));
  data.set('newPassword', md5(newPassword));
  data.set('type', 1);
  return await baseRequest('UPDATE_PASSWORD', data);
}

//消息列表
export const getMessageList = async (messageType: string, roleId: string, page: string, limit: string) => {
  const data = new Map();
  data.set('messageType', messageType);
  // data.set('markNum', markNum); 
  data.set('roleID', roleId);
  data.set('page', page);
  data.set('limit', limit);
  return await baseRequest('MESSAGE_LIST', data);
}

//消息列表已读
export const updataMessageRead = async (messageId: string) => {
  const data = new Map();
  data.set('messageID', messageId);
  return await baseRequest('UPDATA_MESSAGE_READ', data);
}

//删除一条消息
export const deleteMessage = async (messageId: string) => {
  const data = new Map();
  data.set('messageID', messageId);
  return await baseRequest('DELETE_MESSAGE', data);
}

//未读消息总条数
export const getMessageNotReadNum = async (messageType: string) => {
  const data = new Map();
  data.set('messageType', messageType);
  data.set('readStatus', 0);
  return await baseRequest('MESSAGE_NOT_READ_NUM', data);
}

// 获取订单列表
export const getOrderList = async (status: string, page: string, limit: string,orderId: string,address: string,pageType: string) => {
  const data = new Map();
  data.set('orderStatus', status);
  data.set('page', page);
  data.set('limit', limit);
  data.set('isTop',1);
  // data.set('isAssist',0);
  if(orderId){
    data.set('factoryCodeID',orderId);
  }
  if(address){
    data.set('address', new Buffer(address).toString('base64'));
  }
  if(pageType == 'outsource'){ // 外协单
    data.set('otherOrders',1); 
  }
  return await baseRequest('ORDER_LIST', data);
}

// 更新订单
export const updateOrder = async (factoryOrderID: string, factoryOrderStatus: string,driverName: string,driverMobile: string,driverArrival: string,packages: string,factoryID: string) => {
  const data = new Map();
  data.set('factoryOrderID', factoryOrderID);
  data.set('factoryOrderStatus', factoryOrderStatus);
  if(driverName){
    data.set('driverName', new Buffer(driverName).toString('base64'));
  }
  if(driverMobile){
    data.set('driverMobile', driverMobile);
  }
  if(driverArrival){
    data.set('driverArrival', driverArrival);
  }
  if(packages){
    data.set('packages', packages);
  }
  if(factoryID){
    data.set('factoryID', factoryID);
  }
  return await baseRequest('UPDATE_ORDER', data);
}

//上传图片
export const  uploadImage = async (name: string,userID: string, factoryOrderID: string, factoryID: string, file: any,roleID: string) => {
  
  const data = new FormData();
  if (file) {
    let fileName = "";
    if (file && file.uri) {
      fileName = file.uri.slice(file.uri.lastIndexOf("/") + 1, file.uri.lenght);
    }
    data.append("photo", {
      uri: file?.uri,
      type: "multipart/form-data",
      name: fileName
    });
  }
  data.append("userID", userID);
  data.append("roleID", roleID);
  data.append("factoryOrderID", factoryOrderID); 
  data.append("factoryID", factoryID);
  // if(days){
  //   data.append("days", days);
  // }
  data.append("signature", md5("depart" + secretKey).substring(0, 17));
  const url = "http://customize.shengda-wood.com/api/v1/factoryOrder/updateProgress";
  return await request
    .post(url, data)
    .then(response => response)
    .catch(error => error);
}
// 删除图片
export const deleteProgressImage = async (photoName: string, factoryOrderID: string) => {
  const data = new Map();
  data.set('photoName', photoName);
  data.set('factoryOrderID', factoryOrderID);
  return await baseRequest('DELETE_PROGRESS_IMAGE', data);
}

// 获取订单详情
export const getOrderDetail = async (factoryCodeID: string) => {
  const data = new Map();
  data.set('factoryCodeID', factoryCodeID);
  return await baseRequest('ORDERDETAIL', data);
}

// 获取退货单详情
export const getReturnOrderDetail = async (code_id: string) => {
  const data = new Map();
  data.set('quit_cgds_orders_code_id', code_id);
  return await baseRequest('RETURN_ORDER_DETAIL', data);
}

// 确认退货单
export const confirmReturnOrder = async (code_id: string) => {
  const data = new Map();
  data.set('quit_cgds_orders_code_id', code_id);
  return await baseRequest('CONFIRM_RETURN_ORDER', data);
}

// 签收退货订单
export const signReturnOrder = async (code_id: string) => {
  const data = new Map();
  data.set('quit_cgds_orders_code_id', code_id);
  return await baseRequest('SIGN_RETURN_ORDER', data);
}

// 获取app版本信息
export const getAppVersion = async () => {
  return await baseRequest('APP_VERSION_INFOMATION');
}

// 更新生产单预估天数 / 平方数
export const updateFactoryOrder = async (codeID: string,factoryID: string,factoryCodeID: string,days: string,produceTime: string,squareMeter: string,splitUser: string) => {
  const data = new Map();
  data.set('codeID', codeID);
  data.set('factoryID', factoryID);
  data.set('factoryCodeID', factoryCodeID);
  data.set('days', days);
  data.set('produceTime', produceTime);
  if(squareMeter){
    data.set('squareMeter', squareMeter);
  }
  if(splitUser){
    data.set('splitUser', splitUser);
  }
  return await baseRequest('EDITFACTORY', data);
}

// APP 待接单/已接单/生产中  统计数据
export const statisticalData = async () => {
  return await baseRequest('STATIISTICAL_DATA');
}

// 获取采购单列表
export const getPurchaseList = async (page: string, limit: string,keywords: string) => {
  const data = new Map();
  data.set('status', 20);  // 已确认,待入库
  data.set('codeOrSupplier',keywords);
  data.set('page', page);
  data.set('limit', limit);
  return await baseRequest('PURCHASE_LIST', data);
}
// 获取采购单详情
export const getPurchaseDetail = async (cgdOrderID: string) => {
  const data = new Map();
  data.set('cgdOrderID', cgdOrderID);
  return await baseRequest('PURCHASE_Detail', data);
}
// 采购单入库
export const updatePurchase = async (cgdOrderID: string,goods: string) => {
  const data = new Map();
  data.set('cgdOrderID', cgdOrderID);
  data.set('goods', goods);
  return await baseRequest('PURCHASE_UPDATE_STATUS', data);
}

// 获取领料、配料单列表
export const getPickingList = async (page: string, limit: string,status: string,orderType: string,keywords: string) => {
  const data = new Map();
  data.set('status', status);
  data.set('orderType',orderType);
  if(keywords){
    data.set('codeOrAddress',keywords);
  }
  data.set('page', page);
  data.set('limit', limit);
  return await baseRequest('PICKING_LIST', data);
}
// 获取采购单详情
export const getPickingDetail = async (pickUpOrderID: string) => {
  const data = new Map();
  data.set('pickUpOrderID', pickUpOrderID);
  return await baseRequest('PICKING_Detail', data);
}
// 领料、配料 接单
export const updatePickingStatus = async (pickUpOrderID: string,status: string) => {
  const data = new Map();
  data.set('pickUpOrderID', pickUpOrderID);
  data.set('status', status);
  return await baseRequest('PICKING_UPDATE_STATUS', data);
}

// 领料、配料 单个入库
export const updatePickingItemStatus = async (pickUpOrderID: string,detailID: string,status: string,actualNum: string) => {
  const data = new Map();
  data.set('pickUpOrderID', pickUpOrderID);
  data.set('detailID', detailID);
  data.set('status', status); // 10未配 20已配
  data.set('actualNum', actualNum);
  return await baseRequest('PICKING_UPDATE_ITEM_STATUS', data);
}

// 生产排程
export const getFactoryList = async (page: string, limit: string,produceNode: string,factoryCodeID: string,address: string) => {
  const data = new Map();
  data.set('orderStatus', 23);
  if(factoryCodeID){
    data.set('factoryCodeID',factoryCodeID);
  }
  if(address){
    data.set('address', new Buffer(address).toString('base64'));
  }
  if(produceNode){
    data.set('produceNode',produceNode);
  }
  data.set("paiCheng", 1);
  data.set('page', page);
  data.set('limit', limit);
  return await baseRequest('FACTORY_LIST', data);
}
// 更新排程状态
export const updateFactoryStatus = async (code: string,node: string) => {
  const data = new Map();
  data.set('code', code);
  data.set('node', node);
  return await baseRequest('UPDATE_FACTORY', data);
}

// 根据条形码查询包装清单
export const getPackagesInfo = async (packagesCode: string) => {
  const data = new Map();
  data.set('packagesCode', packagesCode);
  return await baseRequest('PACKAGES', data);
}



// 获取拆单人员
export const getDisassemblyUser = async () => {
  const data = new Map();
  data.set('page', 1);
  data.set('limit', 0);
  data.set('userRoleID', [20]);
  return await baseRequest('USER_LIST', data);
}

// 根据条形码查询物料
export const getMaterialList = async (materialId: string) => {
  const data = new Map();
  data.set('page', '1');
  data.set('limit', '10');
  data.set('keywords', new Buffer(materialId).toString('base64'));
  return await baseRequest('MATERIALList', data);
}

export const updateMaterial = async (goodsID: string,fixStock: string) => {
  const data = new Map();
  data.set('goodsID', goodsID);
  data.set('fixStock', fixStock);
  return await baseRequest('UPDATEMATERIAL', data);
}